package morenoapp.model.enums;

import java.util.Comparator;
import morenoapp.model.data.MorenoRespondentFace;

/**
 * Различные типы сортировок респондентов.
 * @author dmitry.mamonov
 */
public enum MorenoRespondentsSortOrder implements Comparator {
    PassNameSex {
        @Override
        public int compare(Object leftObj, Object rightObj) {
            MorenoRespondentFace left = (MorenoRespondentFace) leftObj;
            MorenoRespondentFace right = (MorenoRespondentFace) rightObj;

            int comparePass = compareByPassPollFlag(left, right);
            if (comparePass != 0) {
                return comparePass;
            } else {
                int compareName = left.getName().compareToIgnoreCase(right.getName());
                if (compareName != 0) {
                    return compareName;
                } else {
                    return left.getSex().compareTo(right.getSex());
                }
            }
        }
    },
    PassMaleFemaleName {
        @Override
        public int compare(Object leftObj, Object rightObj) {
            return compareBySexImpl(leftObj, rightObj, +1);
        }
    },
    PassFemaleMaleName {
        @Override
        public int compare(Object leftObj, Object rightObj) {
            return compareBySexImpl(leftObj, rightObj, -1);
        }
    };

    protected int compareBySexImpl(Object leftObj, Object rightObj, int sign) {
        MorenoRespondentFace left = (MorenoRespondentFace) leftObj;
        MorenoRespondentFace right = (MorenoRespondentFace) rightObj;

        int comparePass = compareByPassPollFlag(left, right);
        if (comparePass != 0) {
            return comparePass;
        } else {
            int compareSex = left.getSex().compareTo(right.getSex())*sign;
            if (compareSex!=0){
                return compareSex;
            } else {
                return left.getName().compareToIgnoreCase(right.getName());
            }
        }
    }

    protected int compareByPassPollFlag(MorenoRespondentFace left, MorenoRespondentFace right) {
        return (right.isPassedPoll() ? -1 : 0) + (left.isPassedPoll() ? 1 : 0);
    }
}
